﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace LifeInSharePoint.PromotedLinks.Common.WebParts.PromotedLinks
{
    [ToolboxItemAttribute(false)]
    public class PromotedLinks : WebPart
    {

        [Personalizable(PersonalizationScope.Shared), WebBrowsable(true), System.ComponentModel.Category("LifeInSharePoint Config"),
         WebDisplayName("List Name"), WebDescription("List Name")]
        public string ListName { get; set; }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable(true), System.ComponentModel.Category("LifeInSharePoint Config"),
         WebDisplayName("Title"), WebDescription("Title")]
        public string WebPartTitle { get; set; }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable(true), System.ComponentModel.Category("LifeInSharePoint Config"),
         WebDisplayName("Tile BG Colour"), WebDescription("Tile BG Colour")]
        public string TileBGColour { get; set; }

        protected override void OnInit(EventArgs e)
        {
            this.ChromeType = PartChromeType.None;
            base.OnInit(e);
            this.ExportMode = WebPartExportMode.All;
        }

        protected override void CreateChildControls()
        {
            this.Controls.Add(new LiteralControl("<script type='text/javascript' src='" + SPContext.Current.Site.RootWeb.Url + "/Style Library/LifeInSharePoint.Metro/js/jquery-1.7.1.min.js'></script>"));
            this.Controls.Add(new LiteralControl("<script type='text/javascript' src='" + SPContext.Current.Site.RootWeb.Url + "/Style Library/LifeInSharePoint.Metro/js/PromotedLinks.js'></script>"));
            if (!string.IsNullOrEmpty(TileBGColour))
            {
                this.Controls.Add(new LiteralControl("<style>.tile{background-color:" + TileBGColour +"!important;}</style>"));
            }
            try
            {
                if (!string.IsNullOrEmpty(ListName))
                {
                    SPList _promotedLinksList = SPContext.Current.Web.Lists[ListName];
                    SPQuery _query = new SPQuery();
                    _query.Query = "<OrderBy><FieldRef Name='Order' Ascending='TRUE' /></OrderBy>";
                    SPListItemCollection _linksCollection = _promotedLinksList.GetItems(_query);
                    if (_linksCollection.Count != 0)
                    {
                        if (string.IsNullOrEmpty(WebPartTitle))
                        {
                            WebPartTitle = _promotedLinksList.Title;
                        }
                        this.Controls.Add(new LiteralControl("<div class='PromotedLinksHeader'>" + WebPartTitle + "</div>"));
                        foreach (SPListItem item in _linksCollection)
                        {
                            string Title = Utilities.GetColumnStringValue(item, "Title");
                            string Description = Utilities.GetColumnStringValue(item, "PromotedLinksDescription");
                            string Behaviour = Utilities.GetColumnStringValue(item, "PromotedLinksLaunchBehaviour");
                            string LinkIcon = Utilities.GetColumnStringValue(item, "PromotedLinksIconChoice");
                            string BgColour = Utilities.GetColumnStringValue(item, "PromotedLinksBgColour");
                            SPFieldUrlValue LinkUrl = new SPFieldUrlValue(Utilities.GetColumnStringValue(item, "PromotedLinksLinkLocation"));
                            SPFieldUrlValue ImageUrl = new SPFieldUrlValue(Utilities.GetColumnStringValue(item, "PromotedLinksBackgroundImageLocation"));
                            string BackgroundImage = "";
                            if (string.IsNullOrEmpty(ImageUrl.Url))
                            {
                                BackgroundImage = "<i class='icon-" + LinkIcon.Replace(" ", "-").ToLower() + "'></i>";
                            }
                            else
                            {
                                BackgroundImage = "<img src='" + ImageUrl.Url + "' border='0' />";
                            }

                            string linkAction = "";

                            switch (Behaviour)
                            {
                                case "In page navigation":
                                    linkAction = "";
                                    break;

                                case "Dialog":
                                    linkAction = "onclick=\"return PromotedLinksDialog('" + LinkUrl.Url + "');\"";
                                    break;

                                case "New tab":
                                    linkAction = "target='_blank'";
                                    break;
                            }
                            if (!string.IsNullOrEmpty(BgColour))
                            {
                                BgColour = "style='background-color:" + BgColour + ";'";
                            }

                            this.Controls.Add(
                                new LiteralControl(
                                    string.Format(
                                        "<div class='tile' {5}><a href='{0}' {4}><div class='tileContents'><div class='tileImage'>{1}</div><div class='tileCaption'><h4>{2}</h4><p>{3}</p></div></div></a></div>",
                                        LinkUrl.Url, BackgroundImage, Title, Description, linkAction, BgColour)));
                        }
                    }
                    else
                    {
                        this.Controls.Add(new LiteralControl("No Items Found"));
                    }
                }
                else
                {
                    this.Controls.Add(new LiteralControl("No List Configured"));
                }
            }
            catch (Exception ex)
            {
                this.Controls.Add(new LiteralControl("Please Try Again. " + ex.Message));
            }
        }



    }
}
